﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace WebApplication1
{
    public partial class NewsEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "admin")
            {
                Response.Redirect("Search.aspx");
            }
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                SQLHelper.getRootTags(out dt);
                for (int ix = 0; ix < dt.Rows.Count; ix++)
                {
                    ddlType.Items.Add(new ListItem(dt.Rows[ix]["TagName"].ToString(),dt.Rows[ix]["Id"].ToString()));
                }

                if (Session["NewsId"] == null)
                {

                }
                else
                {
                    int i = Convert.ToInt32(Session["NewsId"]);
                    DataTable m_dt = new DataTable();
                    SQLHelper.getNewsById(i, out m_dt);
                    CKEditor1.Text = m_dt.Rows[0]["NewsContent"].ToString();
                    int ix = 0;
                    while (ddlType.Items[ix].Value != m_dt.Rows[0]["PK_TagInfo"].ToString())
                    {
                        ix++;
                    }
                    if (ix < ddlType.Items.Count)
                        ddlType.Items[ix].Selected = true;
                    tbxTitle.Text = m_dt.Rows[0]["Title"].ToString();

                    
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (tbxTitle.Text == null)
            {
                Response.Write("<script language = javascript>alert('请输入新闻标题');</script>");
                return;
            }
            if (CKEditor1.Text == null)
            {
                Response.Write("<script language = javascript>alert('请输入新闻内容');</script>");
                return;
            }
            if (ddlType.SelectedItem == null)
            {
                Response.Write("<script language = javascript>alert('请选择新闻所属标签');</script>");
                return;
            }

            if (Session["NewsId"] != null)
            {
                int i = (Int32)Session["NewsId"];
                int iresult = SQLHelper.updateNewsById(i, tbxTitle.Text, CKEditor1.Text, DateTime.Now.ToString(), ((Entity.UserInfo)Session["UserInfo"]).UserId, Convert.ToInt32(ddlType.SelectedItem.Value));
                if (iresult == 1)
                {
                    Session.Remove("NewsId");
                    Response.Write("<script>alert('编辑成功!');location.href='NewsManage.aspx';</script>");
                }
                else
                {
                    Session.Remove("NewsId");
                    Response.Write("<script>alert('编辑失败!');location.href='NewsManage.aspx';</script>");
                }
            }
            else
            {
                int iresult = SQLHelper.AddNews(tbxTitle.Text, CKEditor1.Text, DateTime.Now.ToString(), ((Entity.UserInfo)Session["UserInfo"]).UserId, Convert.ToInt32(ddlType.SelectedItem.Value));
                if (iresult == 1)
                {
                    Response.Write("<script>alert('新增成功!');location.href='NewsManage.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('新增失败!');location.href='NewsManage.aspx';</script>");
                }

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("NewsId");
            Response.Redirect("~/NewsManage.aspx");
        }

        
    }
}